package com.nercel.dsj.gksales.service.impl;

import com.nercel.dsj.gksales.dao.mapper.RoleMapper;
import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.dto.RoleParamsDTO;
import com.nercel.dsj.gksales.model.entity.Role;
import com.nercel.dsj.gksales.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sunlin
 * @date 2018-11-16
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleServiceImpl implements RoleService {

	private final RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        roleMapper.insertSelective(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    @Transactional
    public void save(RoleParamsDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        roleMapper.insertSelective(role);
        if (roleDTO.getPermissions() != null) {
            roleMapper.batchInsertPermissions(role.getId(), roleDTO.getPermissions());
        }
    }

    @Override
    public void update(RoleParamsDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        roleMapper.updateByPrimaryKey(role);
        if (roleDTO.getPermissions() != null) {
            roleMapper.updatePermissions(roleDTO.getId(), roleDTO.getPermissions());
        }
    }

    @Override
    public Role findById(Integer integer) {
        return roleMapper.selectByIdWithPermissions(integer);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.query(new QueryParams());
    }

    @Override
    public void delete(Integer integer) {
        roleMapper.deleteByPrimaryKey(integer);
    }

    @Override
    public List<Role> list(QueryParams queryParams) {
        return roleMapper.query(queryParams);
    }

    @Override
    public Role findByRoleName(String name) {
        QueryParams queryParams = new QueryParams();
        queryParams.put("role", name);
        List<Role> list = list(queryParams);
        return list.size() == 1 ? list.get(0) : null;
    }

    @Override
    public int addPermissions(Integer roleId, List<Integer> ids) {
        return roleMapper.batchInsertPermissions(roleId, ids);
    }

    @Override
    public void deletePermission(Integer roleId, Integer permissionId) {
        roleMapper.deletePermission(roleId, permissionId);
    }

    @Override
    public void updatePermissions(Integer roleId, List<Integer> ids) {
        roleMapper.updatePermissions(roleId, ids);
    }

    @Override
    public List<Role> selectByUserIdWithPermissions(Integer id) {
        return roleMapper.selectByUserIdWithPermissions(id);
    }

}
